cmake_minimum_required(VERSION 3.5)
project(rviz_custom_plugins)

# find ament and necessary ROS2 and Qt packages\ nfind_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rviz_common REQUIRED)
find_package(pluginlib REQUIRED)
find_package(std_msgs REQUIRED)
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Network)
find_package(geometry_msgs REQUIRED)
find_package(controller_msgs REQUIRED)
find_package(rviz_default_plugins REQUIRED)
find_package(rviz_rendering REQUIRED)
find_package(nav2_msgs REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_ros REQUIRED)
find_package(angles REQUIRED)
find_package(PCL REQUIRED)
find_package(tf2_eigen REQUIRED)
find_package(pcl_ros REQUIRED)
find_package(rosbag2_cpp REQUIRED)
find_package(tf2_sensor_msgs REQUIRED)
find_package(OpenMP REQUIRED)


pkg_check_modules(FFMPEG REQUIRED libavcodec libavformat libavutil libswscale)

# enable automoc for Qt signals/slots
set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

#add_custom_command(
#    OUTPUT ${CMAKE_SOURCE_DIR}/include/panels/ui_waypoint_panel.h
#    COMMAND uic ${CMAKE_SOURCE_DIR}/ui/waypoint_panel.ui -o ${CMAKE_SOURCE_DIR}/include/panels/ui_waypoint_panel.h
#    DEPENDS ${CMAKE_SOURCE_DIR}/ui/waypoint_panel.ui
#)

add_custom_target(qt_ui
    COMMAND uic ${CMAKE_SOURCE_DIR}/ui/waypoint_panel.ui -o ${CMAKE_SOURCE_DIR}/include/${PROJECT_NAME}/panels/ui_waypoint_panel.h
    COMMAND uic ${CMAKE_SOURCE_DIR}/ui/flyzone_panel.ui -o ${CMAKE_SOURCE_DIR}/include/${PROJECT_NAME}/panels/ui_flyzone_panel.h
    COMMAND uic ${CMAKE_SOURCE_DIR}/ui/snapshot_point_panel.ui -o ${CMAKE_SOURCE_DIR}/include/${PROJECT_NAME}/panels/ui_snapshot_point_panel.h
    COMMAND uic ${CMAKE_SOURCE_DIR}/ui/snapshot_plane_panel.ui -o ${CMAKE_SOURCE_DIR}/include/${PROJECT_NAME}/panels/ui_snapshot_plane_panel.h
    COMMAND uic ${CMAKE_SOURCE_DIR}/ui/file_transfer_dialog.ui -o ${CMAKE_SOURCE_DIR}/include/${PROJECT_NAME}/widgets/ui_file_transfer_dialog.h
    COMMAND uic ${CMAKE_SOURCE_DIR}/ui/command_panel.ui -o ${CMAKE_SOURCE_DIR}/include/${PROJECT_NAME}/panels/ui_command_panel.h
)

file(GLOB_RECURSE SOURCE "include/*.h" "src/*.cpp")

# build shared library
add_library(${PROJECT_NAME} SHARED ${SOURCE})

add_dependencies(${PROJECT_NAME} qt_ui)

target_include_directories(${PROJECT_NAME} PUBLIC
  $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
  $<INSTALL_INTERFACE:include/${PROJECT_NAME}>
  ${FFMPEG_INCLUDE_DIRS}
  ${PCL_INCLUDE_DIRS}
)

# link dependencies
ament_target_dependencies(${PROJECT_NAME} PUBLIC
  rclcpp
  rviz_common
  pluginlib
  std_msgs
  rviz_rendering
  rviz_default_plugins
  geometry_msgs
  nav2_msgs
  controller_msgs
  angles
  tf2
  tf2_eigen
  tf2_sensor_msgs
  pcl_ros
  rosbag2_cpp
  Qt5
  OpenMP
)

target_link_libraries(${PROJECT_NAME} 
  # rviz_common::rviz_common
  # Qt5::Widgets
  PUBLIC ${FFMPEG_LIBRARIES}
  Qt5::Network
  # tf2::tf2
  # tf2_ros::tf2_ros
  # tf2_sensor_msgs::tf2_sensor_msgs
  # PUBLIC OpenMP::OpenMP_CXX
)


ament_export_dependencies(
  pluginlib
  std_msgs
  rviz_rendering
  rviz_default_plugins
  geometry_msgs
  nav2_msgs
  controller_msgs
  angles
  tf2
  tf2_eigen
  tf2_sensor_msgs
  pcl_ros
  rosbag2_cpp
  OpenMP
)
ament_export_include_directories("include/${PROJECT_NAME}")
ament_export_targets(${PROJECT_NAME} HAS_LIBRARY_TARGET)

# export plugin description for rviz
pluginlib_export_plugin_description_file(rviz_common plugin_description.xml)


# install
install(TARGETS ${PROJECT_NAME}
  EXPORT ${PROJECT_NAME}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)

install(DIRECTORY include/
  DESTINATION include/${PROJECT_NAME}
)



install(FILES plugin_description.xml DESTINATION share/${PROJECT_NAME})
install(DIRECTORY media/ DESTINATION share/${PROJECT_NAME}/media)
install(DIRECTORY icons/ DESTINATION share/${PROJECT_NAME}/icons)




# install scripts
# ament_python_install_package(scripts)



ament_package()
